//
//  DateUtils.m
//  uteped_sdk
//
//  Created by mateng on 2024/3/2.
//

#import "DateUtils.h"

@implementation DateUtils

+ (NSTimeInterval)timeToInt:(NSString*)dateString fmt:(NSString*)fmt {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:fmt];
    NSDate *date = [formatter dateFromString:dateString];
    NSTimeInterval interval = [date timeIntervalSince1970];
    
    return interval;
}

+ (NSString *)timeStampToString:(NSInteger)stamp fmt:(NSString*)fmt {
    NSDate *date = [[NSDate alloc]initWithTimeIntervalSince1970:stamp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:fmt];
    NSString  *timeString  = [formatter stringFromDate:date];
    
    return timeString;
}

+ (NSInteger)getTimeInSeconds:(NSDate*)date truncHMS:(BOOL)truncHMS {
    if (truncHMS) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateStyle:NSDateFormatterShortStyle];
        NSString  *timeString  = [formatter stringFromDate:date];
        NSDate *newDate = [formatter dateFromString:timeString];
        return [newDate timeIntervalSince1970];
    } else {
        return [date timeIntervalSince1970];
    }
}

+ (NSInteger)getNowTimeInSeconds {
    NSDate *datenow = [NSDate date];
    return [datenow timeIntervalSince1970];
}

@end
